package com.wfh.mianshiji.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wfh.mianshiji.common.BaseResponse;
import com.wfh.mianshiji.common.ErrorCode;
import com.wfh.mianshiji.common.ResultUtils;
import com.wfh.mianshiji.exception.BusinessException;
import com.wfh.mianshiji.model.dto.resume.ResumeQueryRequest;
import com.wfh.mianshiji.model.entity.Resume;
import com.wfh.mianshiji.model.vo.ResumeVo;
import com.wfh.mianshiji.model.vo.UserVo;
import com.wfh.mianshiji.service.ResumeService;
import com.wfh.mianshiji.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Title: ResumeController
 * @Author wangfenghuan
 * @Package com.wfh.mianshiji.controller
 * @Date 2025/10/30 19:50
 * @description: 简历控制器
 */
@RestController
@RequestMapping("/resume")
public class ResumeController {

    @Resource
    private ResumeService resumeService;

    @Resource
    private UserService userService;

    /**
     * 上传简历
     * @param resume
     * @return
     */
    @PostMapping("/upload")
    public BaseResponse<String> uploadResume(MultipartFile resume, HttpServletRequest request) {
        if (resume == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "简历不能为空");
        }
        String res = resumeService.upload(resume, request);
        return ResultUtils.success(res);
    }

    /**
     * 根据id获取简历
     *
     * @param userId
     * @param resumeId
     * @param request
     * @return
     */
    @GetMapping("/get/{userId}/{resumeId}")
    public BaseResponse<ResumeVo> getById(@PathVariable("userId") String userId, @PathVariable("resumeId") String resumeId, HttpServletRequest request) {
        UserVo currentUser = userService.getCurrentUser(request);
        Long currentUserId = currentUser.getId();
        if (userId == null || resumeId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (!String.valueOf(currentUserId).equals(userId)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "只可以查看自己的简历");
        }
        List<Resume> resumeList = resumeService.getBaseMapper().selectList(new QueryWrapper<>(Resume.class)
                .eq("userId", userId)
                .eq("id", resumeId));
        Resume first = resumeList.getFirst();
        ResumeVo resumeVo = BeanUtil.copyProperties(first, ResumeVo.class);
        return ResultUtils.success(resumeVo);
    }

    /**
     * 分页获取简历
     * @param resumeQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/page")
    public BaseResponse<List<ResumeVo>> getByPage(@RequestBody ResumeQueryRequest resumeQueryRequest, HttpServletRequest request) {
        Long userId = resumeQueryRequest.getUserId();
        long current = resumeQueryRequest.getCurrent();
        long pageSize = resumeQueryRequest.getPageSize();
        UserVo currentUser = userService.getCurrentUser(request);
        if (!userId.equals(currentUser.getId())){
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "只能查看自己的简历列表");
        }
        if (ObjUtil.isEmpty(current) || ObjUtil.isEmpty(pageSize)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户id不能为空");
        }
        Page<Resume> page = resumeService.page(new Page<>(current, pageSize), new QueryWrapper<>(Resume.class).eq("userId", userId));
        List<ResumeVo> list = page.getRecords().stream().map(resume -> BeanUtil.copyProperties(resume, ResumeVo.class)).toList();
        return ResultUtils.success(list);
    }

    /**
     * 根据id删除简历
     * @param userId
     * @param resumeId
     * @return
     */
    @GetMapping("/del/{userId}/{resumeId}")
    public BaseResponse<Boolean> delById(@PathVariable("userId") Long userId, @PathVariable("resumeId") Long resumeId){
        if (userId == null || resumeId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        try {
            resumeService.getBaseMapper().delete(new QueryWrapper<>(Resume.class).eq("userId", userId).eq("id", resumeId));
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return ResultUtils.success(true);
    }
}
